package com.zentrale1.noodlemail;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

/*
 * The MIT License
 * 
 * Copyright (c) 2010 Andreas Fleig
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


/**
 * Converts Maildirs to Weka ARFF files 
 * @author afleig
 *
 */
public class ArffWriter 
{
	private BufferedWriter outWriter = null;
	private Maildir dir;
	private String outfile;
	
	public static void main(String[] args)
	{
		if (args.length < 2) {
			System.out.println("ArffWriter invocation: ArffWriter "
					+ "<Maildir path> <output.arff>");
			return;
		}
		ArffWriter w = new ArffWriter(args[0], args[1]);
		w.prepareFile();
		try {
			w.processAll();
			w.closeFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public ArffWriter(String maildirPath, String outfile)
	{
		this.outfile = outfile;
		this.dir = new Maildir(maildirPath);
	}
	
	public void processAll() throws IOException
	{
		this.processMaildir(this.dir);
	}
	
	public void processMaildir(Maildir dir) throws IOException
	{
		String[] messages = dir.listMessages(false);
		Mail[] mails = new Mail[messages.length];
		
		for (int i=0; i<messages.length; i++) {
			mails[i] = MailFactory.parse(messages[i]);
		}
		Arrays.sort(mails);
		
		for (Mail m : mails) {
			writeMailData(dir.getName(), m);
		}
		
		Maildir[] subdirs = dir.getFolders();
		for (Maildir subdir : subdirs) {
			processMaildir(subdir);
		}
	}
	
	public void writeMailData(String folder, Mail m) throws IOException
	{
		String[] data = new String[3]; // class, subject, body
		
		data[0] = folder;
		
		data[1] = m.getSubject();
		data[1] = data[1].replaceAll("\"", "");
		data[1] = data[1].replaceAll("[\r\n]", " ");
		
		data[2] = m.getTextBody();
		data[2] = data[2].replaceAll("[\r\n]", " ");
		data[2] = data[2].replaceAll("\"", "");
		
		System.out.println("storing "+m.getPath());
		store(data);
	}
	
	public void prepareFile()
	{
		String header = "@relation 'Maildir'\n@attribute class string\n"
						+ "@attribute subject string\n"
						+ "@attribute body string\n@data\n";
		try {
			outWriter = new BufferedWriter(new FileWriter(outfile));
			outWriter.write(header);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	private void store(String[] data) throws IOException
	{
		if (outWriter == null) {
			throw new IOException("ArffWriter.store: no output file, run prepareFile first.");
		}
		String out = "\"" + data[0] + "\",\"" + data[1] + "\",\"" + data[2] + "\"\n"; 
		outWriter.write(out);
	}
	
	public void closeFile() throws IOException
	{
		outWriter.close();
	}
	

}
